package com.wuchao.controller;

import com.wuchao.entity.User;
import com.wuchao.service.serviceImpl.ServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author wuchao66
 * @Date 2022/7/7 10:20
 * @Version 1.0
 */
@WebServlet(name = "ServleELAndJstl",urlPatterns = "/ServleELAndJstl")
public class ServleELAndJstl extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//乱码问题
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        //out对象
        PrintWriter out = response.getWriter();//文件流
        //刷新缓存区 关闭
        out.flush();
        out.close();
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //乱码问题
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        //out对象
        PrintWriter out = response.getWriter();//文件流
        String op = request.getParameter("op");
        ServiceImpl service = new ServiceImpl();
        if(op!=null){
            int id = Integer.parseInt(request.getParameter("id"));
            int update = service.update("delete from user where id=?", id);
            if(update>0){//无论幼稚无知成功失败都要带着最新的集合回去
                request.setAttribute("isdelete","删除成功");
                List<User> list = service.findPoJo("select  * from user");
                request.setAttribute("list",list);//带值过去了
                request.getRequestDispatcher("demo8/main.jsp")
                        .forward(request,response);
            }else{
                request.setAttribute("isdelete","删除失败");
                List<User> list = service.findPoJo("select  * from user");
                request.setAttribute("list",list);//带值过去了
                request.getRequestDispatcher("demo8/main.jsp")
                        .forward(request,response);
            }
        }else{
            List<User> list = service.findPoJo("select  * from user");
            request.setAttribute("list",list);//带值过去了
            request.getRequestDispatcher("demo8/main.jsp")
                    .forward(request,response);
        }




      /*  //刷新缓存区 关闭
        //普通变量
        request.setAttribute("bl",2);//k-v
        //对象
        User user = new User();
        user.setName("标签库");
        user.setSex(-3);
        user.setHobby("我就爱霸凌你，咋的了？");
        request.setAttribute("user",user);
        //list
        List<User> list=new ArrayList<>();
        list.add(user);
        request.setAttribute("list",list);
        //map
        Map<String,User> map=new HashMap<>();
        map.put("key",user);
        request.setAttribute("map",map);
        request.getRequestDispatcher("demo8/el.jsp")
                .forward(request,response);*/
        out.flush();
        out.close();

    }
    public void init(){
        System.out.println("初始化");
    }
    public void destroy(){
        System.out.println("摧毁");
    }
}
